package com.action.app_landing;

import java.io.File;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.enetiy.Useraccount;
import com.enetiy.Userinfo;
import com.enetiy.Users;
import com.opensymphony.xwork2.ActionSupport;
import com.service.LoginService;
import com.service.UseraccountService;
import com.service.UserimgService;
import com.service.UserinfoService;
import com.util.IPUtil;
import com.util.Md5Util;

public class FaultLoginAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String phone;
	private String pwd;
	private String token;
	private int userid;
	private int id;
	private UseraccountService useraccountService;
	private UserinfoService userinfoservice;
	
	public void userVerify(){
		userid=id;
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		JSONObject json = new JSONObject();
		PrintWriter out=null;
		Useraccount account=null;
		try {
			out = response.getWriter();
			if(token!=null){
				Userinfo userinfo=null;
				if((userinfo=userinfoservice.selectUserToken(token, userid))!=null){
					 List<Useraccount> useraccounts = useraccountService.selectUseraccounts(userid,1);
					this.success(userinfo.getUsers(),json,useraccounts);
				}else{
					json.put("code", 0);
				}
			}else{
				if((account=useraccountService.selectUseraccount(phone)) != null&&account.getUsers().getPwd().equals(pwd)){
					List<Useraccount> useraccounts = useraccountService.selectUseraccounts(userid,1);
					this.success(account.getUsers(), json,useraccounts);
				}else{
					json.put("code", 0);
				}
			}
			out.print(json);
		} catch (Exception e) {
			json.put("code", -1);
			out.print(json);
			e.printStackTrace();
		}
	}
	
	private void success(Users u,JSONObject json,List<Useraccount> useraccounts){
		if(u.getHeadImg()!=null){
			json.put("code", 1);
			json.put("headurl",IPUtil.getIp()+"/img/uersImg/headImg/"+u.getHeadImg());
			json.put("user", useraccounts.get(0).getAccount());
			json.put("nickname", u.getNickname());
		}else{
			String realPath = IPUtil.getFilePath()+"/img/uersImg/headImg/MoRenHead/";
			File f = new File(realPath);
			//路劲底所有图片
			String[] list = f.list();
			json.put("code", 1);
			json.put("user", useraccounts.get(0).getAccount());
			json.put("headurl",IPUtil.getIp()+"/img/uersImg/headImg/MoRenHead/"+list[0]);
			json.put("nickname", u.getNickname());
		}
	}
	
	
	public void setToken(String token) {
		this.token = token;
	}

	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	

	

	public void setId(int id) {
		this.id = id;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public void setUseraccountService(UseraccountService useraccountService) {
		this.useraccountService = useraccountService;
	}

	public void setUserinfoservice(UserinfoService userinfoservice) {
		this.userinfoservice = userinfoservice;
	}
	
}
